package com.security.customer;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Component;

/**
 * @Author: TongRui乀
 * @Date: 2020/3/21 9:19
 * @description：  自定义获取用户的逻辑
 */
@Slf4j
@Component
public class CustomerUserDetailService implements UserDetailsService {

    @Autowired
    private PasswordEncoder passwordEncoder;

    @Override
    public UserDetails loadUserByUsername(String s) throws UsernameNotFoundException {

        log.info("登录用户：{}：认证获取用户信息的自定义逻辑", s);

        // 密码加密

        String encodePassword = passwordEncoder.encode("123456");

        // 这里的User 对象是SpringSecurity提供的实现了UserDetail接口的实现 我们可以自定义实现
        return new User("jack", encodePassword, AuthorityUtils.commaSeparatedStringToAuthorityList("ADMIN"));
    }
}
